package org.adw.launcher2.ld.secret.db;
// trong project chua tinh toi viec su dung class nay, do nhu cau cua de bai la chi xoa callLog khi active ma thoi
// da xoa trong content provider
import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.dom.MyCallLog;
import org.adw.launcher2.ld.secret.ui.utils.Global;


import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.CallLog;
import android.util.Log;

public class MyCallLogCache {
	public static final String TAG = "MyCallLogCache";
	
	private static final String TABLE_MY_CALL_LOG = "my_call_log";
	private static final String CREATE_MY_CALL_LOG_TABLE_QUERY =
		"create table " + TABLE_MY_CALL_LOG + " (numbertype, new, duration, _id, " +
				"numberlabel, name, number, type, date)";
	private static final String INSERT_MY_CALL_LOG = 
		"insert into " + TABLE_MY_CALL_LOG + " (numbertype, new, duration, _id, " +
				"numberlabel, name, number, type, date) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private static final String QUERY_MY_CALL_LOG_BY_ID = 
		"select numbertype, new, duration, _id, " +
				"numberlabel, name, number, type, date from " + TABLE_MY_CALL_LOG + 
				" where _id = ?";
	private static final String QUERY_ALL_MY_CALL_LOGS = 
		"select numbertype, new, duration, _id, " +
				"numberlabel, name, number, type, date from " + TABLE_MY_CALL_LOG;
	
	private static MyCallLogCache instance = null;
	private SQLiteDatabase db;
	private Context context;
	
	public static MyCallLogCache getInstance(DatabaseHelper dbHelper, Context context) {
		if (instance == null) {
			instance = new MyCallLogCache(dbHelper, context);
		}
		
		return instance;
	}
	
	public MyCallLog getMyCallLogById(long id) {
		MyCallLog myCallLog = null;
		
		Cursor cursor = db.rawQuery(QUERY_MY_CALL_LOG_BY_ID, new String[] {"" + id});
		if (cursor.moveToFirst()) {
			myCallLog = new MyCallLog(cursor);
		}
		cursor.close();
		
		return myCallLog;
	}

	private boolean isNeedBackup = true;
	public void backupAllMyCallLogs() {//back up toan bo callLog vao DB table_my_call_log
		createTableIfNecessary();
		
		if (!isNeedBackup) {
			return;
		}
		
		new DbBatchOperation(this.db) {
			
			@Override
			public void doBatchInsert() {
				Cursor cursor = context.getContentResolver().query(
			    		CallLog.Calls.CONTENT_URI, null, null, null, null);
		        if (cursor.moveToFirst()) {
		        	isNeedBackup = false;
		            do {
		                insertMyCallLog(new MyCallLog(cursor));
		            } while (cursor.moveToNext());
	
		        }
		        cursor.close();
			}
		};
	}
	
	
	public List<MyCallLog> getAllMyCallLogs() {
		Cursor cursor = db.rawQuery(QUERY_ALL_MY_CALL_LOGS, null);
		List<MyCallLog> callLogs = new LinkedList<MyCallLog>();
		if (cursor.moveToFirst()) {
			do {
				callLogs.add(new MyCallLog(cursor));
			} while (cursor.moveToNext());
		}
		
		cursor.close();
		return callLogs;
	}

	private MyCallLogCache(DatabaseHelper dbHelper, Context context) {
		db = dbHelper.db;
		this.context = context;
		
		createTableIfNecessary();
	}
	
	private void createTableIfNecessary() {
		if (!Global.dbHelper.isTableExists(TABLE_MY_CALL_LOG)) {
			db.execSQL(CREATE_MY_CALL_LOG_TABLE_QUERY);
		}
	}
	
	private void insertMyCallLog(MyCallLog myCallLog) {
		db.execSQL(INSERT_MY_CALL_LOG, new String[] {
				myCallLog.numbertype,
				myCallLog._new + "",
				myCallLog.duration + "",
				myCallLog.id + "",
				myCallLog.numberlabel,
				myCallLog.name,
				myCallLog.number,
				myCallLog.type,
				myCallLog.date + ""
		});
	}
	public int getCallLogId(){
		int id = -1;
		Cursor cursor = context.getContentResolver().query(
	    		CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls._ID}, null, null, null);
        if (cursor.moveToFirst()) {
        	id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
        }
        cursor.close();
        return id;
	}
	public void deleteAllCallLog(){
		Global.cr.delete(CallLog.Calls.CONTENT_URI, null, null);
	}
}
